package com.order.frontend.order.utils.cs.Other;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class WebSocketSessionManager {
    /**
     * 使用WebSocketSession管理用户状态
     *
     */
    // 使用ConcurrentHashMap存储在线用户
    private static final Map<String, WebSocketSession> userSessions = new ConcurrentHashMap<>();
    public void addUser(String userId, WebSocketSession session) {
        userSessions.put(userId, session);
    }

    public void removeUser(String userId) {
        userSessions.remove(userId);
    }

    public boolean isUserOnline(String userId) {
        WebSocketSession session = userSessions.get(userId);
        return session != null && session.isOpen();
    }

    public WebSocketSession getUserSession(String userId) {
        return userSessions.get(userId);
    }

}
